#include <jni.h>
#include <string>
#include "WIPlayer.h"

JavaVM *javaVM;
JavaCallBack *listener = NULL;
WIPlayer *player = NULL;
WIPlayState *playState = NULL;

extern "C"
JNIEXPORT void JNICALL
Java_com_zhongcheng_superaudioplayer_WIPlayer_WIPlayer_n_1preare(JNIEnv *env, jobject instance,
                                                                 jstring source_) {
    const char *source = env->GetStringUTFChars(source_, 0);
    //准备阶段
    if (player == NULL) {
        listener = new JavaCallBack(javaVM, env, env->NewGlobalRef(instance));
        playState = new WIPlayState();
        player = new WIPlayer(playState, source, listener);
        player->prepare();
    }

}


extern "C"
JNIEXPORT void JNICALL
Java_com_zhongcheng_superaudioplayer_WIPlayer_WIPlayer_n_1start(JNIEnv *env, jobject instance) {
    //开始解码
    if (player != NULL) {
        player->start();
    }
}

extern "C"
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
    jint result = -1;
    javaVM = vm;
    JNIEnv *env;
    //判断JavaVM获取是否成功
    if (vm->GetEnv((void **) &env, JNI_VERSION_1_4) != JNI_OK) {
        return result;
    }
    return JNI_VERSION_1_4;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_zhongcheng_superaudioplayer_WIPlayer_WIPlayer_n_1pause(JNIEnv *env, jobject instance) {
    player->audio->pause();

}

extern "C"
JNIEXPORT void JNICALL
Java_com_zhongcheng_superaudioplayer_WIPlayer_WIPlayer_n_1resum(JNIEnv *env, jobject instance) {
    player->audio->resum();

}

bool isStoping = false;

extern "C"
JNIEXPORT void JNICALL
Java_com_zhongcheng_superaudioplayer_WIPlayer_WIPlayer_n_1stop(JNIEnv *env, jobject instance) {
    if (isStoping){
        return;
    }
    isStoping = true;

    player->release();

    delete player;

    player = NULL;

    playState = NULL;

    listener = NULL;

    isStoping = false;
}extern "C"
JNIEXPORT void JNICALL
Java_com_zhongcheng_superaudioplayer_WIPlayer_WIPlayer_n_1seek(JNIEnv *env, jobject instance,
                                                               jint sceds) {

    if (player!=NULL){
        player->seek(sceds);
    }

}extern "C"
JNIEXPORT jint JNICALL
Java_com_zhongcheng_superaudioplayer_WIPlayer_WIPlayer_n_1duration(JNIEnv *env, jobject instance) {
    int ret = 0;
    if (player!=NULL){
        ret = player->audio->duration;
    }
    return ret;
}extern "C"
JNIEXPORT void JNICALL
Java_com_zhongcheng_superaudioplayer_WIPlayer_WIPlayer_n_1volume(JNIEnv *env, jobject instance,
                                                                 jint vulume) {

    if (player!=NULL){
        player->setVolume(vulume);
    }


}

extern "C"
JNIEXPORT void JNICALL
Java_com_zhongcheng_superaudioplayer_WIPlayer_WIPlayer_n_1setMute(JNIEnv *env, jobject instance,
                                                                  jint mute) {
    if (player!=NULL){
        player->setMute(mute);
    }
}

extern "C"
JNIEXPORT void JNICALL
Java_com_zhongcheng_superaudioplayer_WIPlayer_WIPlayer_n_1setPitch(JNIEnv *env, jobject instance,
                                                                   jdouble pitch) {
    if (player!=NULL){
        player->setpitch(pitch);
    }
}

extern "C"
JNIEXPORT void JNICALL
Java_com_zhongcheng_superaudioplayer_WIPlayer_WIPlayer_n_1settempo(JNIEnv *env, jobject instance,
                                                                   jdouble tempo) {
    if (player!=NULL){
        player->settempo(tempo);
    }
}

extern "C"
JNIEXPORT void JNICALL
Java_com_zhongcheng_superaudioplayer_WIPlayer_WIPlayer_n_1startstoprecord(JNIEnv *env,
                                                                          jobject instance,
                                                                          jboolean start) {

     player->startRecord(start);

}

extern "C"
JNIEXPORT jint JNICALL
Java_com_zhongcheng_superaudioplayer_WIPlayer_WIPlayer_n_1samplerate(JNIEnv *env,
                                                                     jobject instance) {

    return player->getSampleRate();


}